]> git.r.bdr.sh - rbdr/captura/blobdiff - Captura/Core Extensions/CVImageBuffer+cgImage.swift
Add new icons
[rbdr/captura] / Captura / Core Extensions / CVImageBuffer+cgImage.swift
index f8958f22eb5a89ab956abba7c784faf9560ec92f..3f59b8ec1006a023fed8dcdd31167672521e8ff7 100644 (file)
@@ -3,10 +3,19 @@ import ReplayKit
 
 extension CVImageBuffer {
   
-  static let sharedContext = CIContext()
+  private static let contextQueue = DispatchQueue(label: "com.example.contextQueue")
+  static let sharedContext: CIContext = {
+          return CIContext()
+  }()
   
   var cgImage: CGImage? {
-    let ciImage = CIImage(cvImageBuffer: self)
-    return CVImageBuffer.sharedContext.createCGImage(ciImage, from: CGRect(x: 0, y: 0, width: CVPixelBufferGetWidth(self), height: CVPixelBufferGetHeight(self)))
+    var result: CGImage?
+    CVImageBuffer.contextQueue.sync {
+      let ciImage = CIImage(cvImageBuffer: self)
+      let width = CVPixelBufferGetWidth(self)
+      let height = CVPixelBufferGetHeight(self)
+      result = CVImageBuffer.sharedContext.createCGImage(ciImage, from: CGRect(x: 0, y: 0, width: width, height: height))
+    }
+    return result
   }
 }